package com.kun.util;

import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.thymeleaf.context.Context;

import com.kun.service.BookService;
import com.kun.service.UserLikeCommentService;
import com.kun.service.UserService;
/**
 * 定时任务
 * @author KM
 * 2018年3月16日
 */
@SuppressWarnings("deprecation")
@Configuration 
public class SchudelTime {
	
	@Autowired
	private UserLikeCommentService userLikeCommentService;
	
	@Autowired
	private UserService  userServiceImpl;
	
	@Autowired
	private BookService  bookServiceImpl;
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Scheduled(fixedRate = 1000 * 60 * 3, initialDelay = 1000) //每隔3分钟执行一次，延迟1秒执行
	public void updatePayRecords() {
		logger.info("同步redis点赞数据到Mysql（3min/lazy 1s）: The time is now " + new Date().toLocaleString());
		String result = userLikeCommentService.SchudelSync();
		logger.info("同步redis点赞数据到Mysql------>" + result);
	}
	
	/*@Scheduled(fixedRate = 1000 * 60 * 60 * 6, initialDelay = 1000) //每隔6小时执行一次，延迟1秒执行
	public void updateShare() {
		logger.info("更新首页共享排行（6hours/lazy 1s）: The time is now " + new Date().toLocaleString());
		String jsonString = "{level:'desc'}";
		Map<String, Object> map  = userServiceImpl.selectAllUser(1, 5, jsonString, false);
		Context context = new Context();
		context.setVariable("userList", map.get("userList"));
		String msg = StaticPageGenerateUtil.generatePage("generation/shareRank", "shareRank", context);
		logger.info("更新首页共享排行------>" + msg);
	}
	
	
	@Scheduled(fixedRate = 1000 * 60 * 60, initialDelay = 1000) //每隔1小时执行一次，延迟1秒执行
	public void updateBorrow() {
		logger.info("更新首页借阅排行（6hours/lazy 1s）: The time is now " + new Date().toLocaleString());
		String jsonString = "{borrow:'desc'}";
		Map<String, Object> map  = bookServiceImpl.selectAllBook(1, 5, jsonString, false);
		Context context = new Context();
		context.setVariable("bookList", map.get("bookList"));
		String msg = StaticPageGenerateUtil.generatePage("generation/borrowRank", "borrowRank", context);
		logger.info("更新首页借阅排行------>" + msg);
	}*/
	
	
	
//	/**
//	 * cron内容分别是:秒，分，时，天，月，星期，年(可以省略年)
//	 */
//	@Scheduled(cron = "0/20 * * * * ?") // 每隔20秒执行一次
//	public void doTask() {
//		System.out.println("-----每隔20秒执行一次 at:" + new Date().toLocaleString());
//	}
//
//	@Scheduled(fixedRate = 1000 * 60 * 3) // 每隔30秒执行一次
//	public void reportCurrentTime() {
//		System.out.println("每隔30秒执行一次: The time is now " + new Date().toLocaleString());
//	}
//
//	@Scheduled(fixedRate = 1000 * 60, initialDelay = 1000) // 每隔1分钟执行一次，延迟1秒执行
//	public void updatePayRecords() {
//		System.out.println("每隔1分钟执行一次，延迟1秒执行: The time is now " + new Date().toLocaleString());
//	}
}